package app.base.systemmanage.systemmodulesmanage;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import app.base.common.Utility;

import app.base.systemmanage.systemmodulesmanage.dao.SysModulesDAO;
import app.base.systemmanage.systemmodulesmanage.model.SysModules;

public class SystemModulesLayerQuery extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
	    request.setCharacterEncoding(Utility.CHARSET_ENCODING_UTF8);
	    response.setContentType(Utility.DEFAULT_CONTENT_TYPE);

		PrintWriter out = response.getWriter(); 
		
		String node = request.getParameter("node");
		String leaf = request.getParameter("leaf");
		String module = request.getParameter("module");
		
	    if (!(node == null || node.trim().length() == 0))
	    {
		    node = URLDecoder.decode(node, Utility.CHARSET_ENCODING_UTF8).trim();
		    
			if (!(module == null))
			{
				module = URLDecoder.decode(module, Utility.CHARSET_ENCODING_UTF8).trim();
				node = node.substring(0, node.length() - module.length());
			}
	    	
			if (leaf == null || leaf.trim().length() == 0)
			{
				leaf = "2";
			}
			else
			{
				leaf = URLDecoder.decode(leaf, Utility.CHARSET_ENCODING_UTF8).trim();
			}
			
		    out.println(getTreeSubNodesByNodeId(node, leaf, module).toString());
	    }
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
		doGet(request, response);
	}
	
	//return JSON format.
	@SuppressWarnings("unchecked")
	private String getTreeSubNodesByNodeId(String nodeId, String leaf, String module)
	{
		String json = "";
		
		ApplicationContext context = new ClassPathXmlApplicationContext(Utility.DEFAULT_SPRINGFRAMEWORK_APPLICATION_CONTEXT_FILE_PATH);
		SysModulesDAO sysModulesDAO = (SysModulesDAO)context.getBean(Utility.SYSTEM_MODULES_MANAGE_MODULE_BEAN_NAME);
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("MOD_PARENT_ID", nodeId);
		map.put("LEAF_NODE_FLAG", leaf);
		
		List<SysModules> list = sysModulesDAO.select_0001(map);
		
		if (list.size() > 0)
		{
			json = "[";
			
			int index = 0;
			
			for(; index < list.size(); index++){
				SysModules sysModules = (SysModules) list.get(index);
				
				String modId = (module == null) ? sysModules.getMOD_ID().trim() : sysModules.getMOD_ID().trim() + module;
				String modName = sysModules.getMOD_NAME().trim();
				String modDesc = sysModules.getMOD_DESC() == null ? "" : sysModules.getMOD_DESC().trim();
				String isLeaf = sysModules.getLEAF_NODE_FLAG() == null ? "" : sysModules.getLEAF_NODE_FLAG().trim();
				String normalIcon = sysModules.getNORMAL_IMG_URL() == null ? "" : sysModules.getNORMAL_IMG_URL().trim();
				String expandIcon = sysModules.getEXPAND_IMG_URL() == null ? "" : sysModules.getEXPAND_IMG_URL().trim();
				String navUrl = sysModules.getMOD_CFG_PATH() == null ? "" : sysModules.getMOD_CFG_PATH().trim();
				
				String modParentId = sysModules.getMOD_PARENT_ID().trim();
				String displayRankIndex = sysModules.getDISPLAY_RANKING_INDEX().toString().trim();
				String sysHoldFlag = sysModules.getSYS_HOLD_FLAG().trim();

				json += "{id: '" + modId + "',";
				json += "text: '" + modName + "',";
				if (!modName.equals(""))
				{
					json += "qtip: '" + modDesc + "',";
				}
				if (isLeaf.equals("1"))
				{
					json += "leaf: true,";
					json += "actionModuleConfig: '" + navUrl + "',";
				}
				else
				{
					json += "leaf: false,";
				}
				json += "expandIcon: '" + expandIcon + "',";

				json += "icon: '" + normalIcon + "',";
				
				json += "modParentId: '" + modParentId + "',";
				json += "displayRankIndex: '" + displayRankIndex + "',";
				json += "sysHoldFlag: '" + sysHoldFlag + "',";
				json += "singleClickExpand: true";

				json += "}";
				
				if (index < list.size() - 1)
				{
					json += ", ";
				}
			}
			json += "]";
		}
		
		return json;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}